package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.zretc.dbtools.RandomIdTools;
import com.zretc.device.dao.Device_ReturnDao;
import com.zretc.device.entity.Device_Return;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Device_ReturnAddDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtreturn_id;
	private JTextField txtdevice_code;
	private JTextField txtreturn_date;
	private JTextField txtnormal_num;
	private JTextField txtdamage_num;
	private JTextField txtkeeper;
	private JTextField txtreturn_person;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Device_ReturnAddDialog dialog = new Device_ReturnAddDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Device_ReturnAddDialog() {
		setBounds(100, 100, 796, 585);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel label = new JLabel("还库添加");
			label.setFont(new Font("宋体", Font.PLAIN, 40));
			label.setBounds(289, 29, 182, 47);
			contentPanel.add(label);
		}
		{
			txtreturn_id = new JTextField();
			txtreturn_id.setText(RandomIdTools.getRan());
			txtreturn_id.setEditable(false);
			txtreturn_id.setFont(new Font("宋体", Font.PLAIN, 25));
			txtreturn_id.setColumns(10);
			txtreturn_id.setBounds(315, 89, 250, 30);
			contentPanel.add(txtreturn_id);
		}
		{
			JLabel label = new JLabel("归还单号");
			label.setFont(new Font("宋体", Font.PLAIN, 25));
			label.setBounds(181, 89, 120, 30);
			contentPanel.add(label);
		}
		{
			txtdevice_code = new JTextField();
			txtdevice_code.setFont(new Font("宋体", Font.PLAIN, 25));
			txtdevice_code.setColumns(10);
			txtdevice_code.setBounds(315, 135, 250, 30);
			contentPanel.add(txtdevice_code);
		}
		{
			JLabel label = new JLabel("设备编号");
			label.setFont(new Font("宋体", Font.PLAIN, 25));
			label.setBounds(181, 135, 120, 30);
			contentPanel.add(label);
		}
		{
			txtreturn_date = new JTextField();
			txtreturn_date.setFont(new Font("宋体", Font.PLAIN, 25));
			txtreturn_date.setColumns(10);
			txtreturn_date.setBounds(315, 178, 250, 30);
			contentPanel.add(txtreturn_date);
		}
		{
			JLabel label = new JLabel("归还日期");
			label.setFont(new Font("宋体", Font.PLAIN, 25));
			label.setBounds(181, 178, 120, 30);
			contentPanel.add(label);
		}
		{
			txtnormal_num = new JTextField();
			txtnormal_num.setFont(new Font("宋体", Font.PLAIN, 25));
			txtnormal_num.setColumns(10);
			txtnormal_num.setBounds(315, 221, 250, 30);
			contentPanel.add(txtnormal_num);
		}
		{
			JLabel label = new JLabel("正常数量");
			label.setFont(new Font("宋体", Font.PLAIN, 25));
			label.setBounds(181, 221, 133, 30);
			contentPanel.add(label);
		}
		{
			txtdamage_num = new JTextField();
			txtdamage_num.setFont(new Font("宋体", Font.PLAIN, 25));
			txtdamage_num.setColumns(10);
			txtdamage_num.setBounds(315, 264, 250, 30);
			contentPanel.add(txtdamage_num);
		}
		{
			JLabel label = new JLabel("损坏数量");
			label.setFont(new Font("宋体", Font.PLAIN, 25));
			label.setBounds(181, 264, 106, 30);
			contentPanel.add(label);
		}
		{
			txtkeeper = new JTextField();
			txtkeeper.setFont(new Font("宋体", Font.PLAIN, 25));
			txtkeeper.setColumns(10);
			txtkeeper.setBounds(315, 307, 250, 30);
			contentPanel.add(txtkeeper);
		}
		{
			JLabel label = new JLabel("仓库员工");
			label.setFont(new Font("宋体", Font.PLAIN, 25));
			label.setBounds(181, 307, 120, 30);
			contentPanel.add(label);
		}
		{
			txtreturn_person = new JTextField();
			txtreturn_person.setFont(new Font("宋体", Font.PLAIN, 25));
			txtreturn_person.setColumns(10);
			txtreturn_person.setBounds(315, 350, 250, 30);
			contentPanel.add(txtreturn_person);
		}
		{
			JLabel label = new JLabel("归 还 人");
			label.setFont(new Font("宋体", Font.PLAIN, 25));
			label.setBounds(181, 350, 120, 30);
			contentPanel.add(label);
		}
		{
			JButton button = new JButton("取消");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					txtreturn_id.setText(RandomIdTools.getRan());
					txtdevice_code.setText("");
					txtreturn_date.setText("");
					txtkeeper.setText("");
					txtnormal_num.setText("");
					txtdamage_num.setText("");
					txtreturn_person.setText("");
				}
			});
			button.setFont(new Font("宋体", Font.PLAIN, 25));
			button.setBounds(213, 439, 150, 38);
			contentPanel.add(button);
		}
		{
			JButton button = new JButton("添加");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// 还库编号
					String treturn_id=txtreturn_id.getText();
					// 设备编号
					String device_code=txtdevice_code.getText();
					// 归还日期
					String return_date=txtreturn_date.getText();
					// 可以正常使用的数量
					String tnormal_num=txtnormal_num.getText();
					// 损坏的数量
					String tdamage_num=txtdamage_num.getText();
					// 仓库管理者
					String keeper=txtkeeper.getText();
					// 归还人
					String return_person=txtreturn_person.getText();
					
					//判断输入的值
					if (treturn_id.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "编号不能为空");
					} else if (device_code.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "设备编号不能为空");
					} else if (return_date.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "归还日期不能为空");
					} else if (tnormal_num.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "正常数量不能为空");
					} else if (tdamage_num.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "损坏数量不能为空");
					} else if (keeper.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "仓库员工不能为空");
					} else if (return_person.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "归还人不能为空");
					}else {
						// 还库编号
						int return_id=Integer.parseInt(treturn_id);
						// 可以正常使用的数量
						int normal_num=Integer.parseInt(tnormal_num);
						// 损坏的数量
						int damage_num=Integer.parseInt(tdamage_num);
						//创建对象
						Device_Return dreturn = new Device_Return(return_id, device_code, return_date, normal_num, damage_num, keeper, return_person);
						Device_ReturnDao drd = new Device_ReturnDao();
						//调用增加的方法
						boolean flag=drd.addDevReturn(dreturn);
						if (flag) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "添加成功");
						}else {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "添加失败,该单号已添加，请勿重复添加");
						}
					}
				}
			});
			button.setFont(new Font("宋体", Font.PLAIN, 25));
			button.setBounds(415, 439, 150, 38);
			contentPanel.add(button);
			//居中
			setLocationRelativeTo(null);
		}
	}

}
